package com.ww.idiom.activity;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.ww.core.util.ImageUtil;
import com.ww.core.util.Logger;
import com.ww.core.util.StringUtils;
import com.ww.core.widget.DetailView;
import com.ww.idiom.R;
import com.ww.idiom.db.IdiomDbHelper;
import com.ww.idiom.entity.Idiom;


/**
 * @since 
 * @version 2013-12-25
 * @author wangwei
 */
public class IdiomTask extends AsyncTask<View, Void, Idiom> {
	private DetailView textView;
	private Context context;
	private Button imageView;
	private ProgressBar oahprogressbar;

	public IdiomTask(Context context) {
		this.context = context;
	}

	protected Idiom doInBackground(View... views) {
		Idiom bm = null;
		View view = views[0];
		if (view.getTag() != null) {
			bm = getIdiom(view.getTag().toString());
		}
		this.oahprogressbar = (ProgressBar) view.findViewById(R.id.oahprogressbar);
		this.textView = (DetailView) view.findViewById(R.id.img);
//		this.textView.setMovementMethod(LinkMovementMethod.getInstance());
		this.imageView = (Button) view.findViewById(R.id.store);
		return bm;
	}

	protected void onPostExecute(Idiom bm) {
		this.textView.setVisibility(View.VISIBLE);
		this.oahprogressbar.setVisibility(View.GONE);
		if (bm != null) {
			String str = "成语名称: " + bm.getName();
			str += "<br><br>详细解释<br>" + bm.getDetailmean();
			if (StringUtils.isNotEmpty(bm.getSynAnt())) {
				str += "<br><br>同反义词<br>" + bm.getSynAnt();
			}
			if (StringUtils.isNotEmpty(bm.getBaike())) {
				str += "<br><br>百科释义<br>" + bm.getBaike();
			}

			textView.setText(str);
			Logger.info("bm.isStore():" + bm.isStore());
			if (bm.isStore()) {
				imageView.setText("已收藏");
				Drawable db = ImageUtil.getDrawable(context, R.drawable.icon_tab_store_yes);
				db.setBounds(0, 0, db.getMinimumWidth(), db.getMinimumHeight());
				imageView.setCompoundDrawables(db, null, null, null);
			} else {
				imageView.setText("未收藏");
				Drawable db = ImageUtil.getDrawable(context, R.drawable.icon_tab_store_no);
				db.setBounds(0, 0, db.getMinimumWidth(), db.getMinimumHeight());
				imageView.setCompoundDrawables(db, null, null, null);
			}
			this.textView = null;
			this.imageView = null;
		}
	}

	public Idiom getIdiom(String fileUrl) {
		try {
			IdiomDbHelper s = new IdiomDbHelper(context);
			return s.getWord(fileUrl);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}